package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.mapper.OrderSettingMapper;
import com.xr.pojo.OrderSetting;
import com.xr.pojo.OrderSettingExample;
import com.xr.service.OrderSettingService;
import com.xr.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约设置业务实现类
 */
@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * 1.批量添加预约设置数据
     * @param orderSettings
     */
    @Transactional
    @Override
    public void addBatch(List<OrderSetting> orderSettings) throws CustomException {
        //遍历集合:筛选数据
        //1.声明一个新的集合，用于保存新增的数据
        List<OrderSetting> addData=new ArrayList<>();
        for (OrderSetting os:orderSettings){
            //1.1 通过日期判断此数据是否已经存在数据库中
            OrderSettingExample ose=new OrderSettingExample();
            OrderSettingExample.Criteria osec = ose.createCriteria();
            osec.andOrderdateEqualTo(os.getOrderdate());
            List<OrderSetting> list = orderSettingMapper.selectByExample(ose);
            if (list!=null && list.size()>0){
                //1.1.1 存在
                //a.判断已经预约人数是否大于可预约的人数
                //b.取出数据
                OrderSetting setting = list.get(0);
                if (setting.getReservations()!=null && setting.getReservations()>os.getNumber()){
                    //大于,则报错
                    throw new CustomException(MessageConstant.ORDERSETTING_FAIL);
                }else{
                    //小于，则更新数据
                    os.setId(setting.getId());
                    orderSettingMapper.updateByPrimaryKeySelective(os);
                }
            }else{
                //1.1.2 不存在
                addData.add(os);
            }
        }
        //批量添加
        if (addData.size()>0){
            orderSettingMapper.addBatch(addData);
        }
    }

    @Override
    public List<Map> getOrderSettingByDate(int year, int month) {
        //1.开始日期
        Date firstDayOfMonth = DateUtils.getFirstDayOfMonth(year, month);
        //2.结束日期
        Date lastDayOfMonth = DateUtils.getLastDayOfMonth(year, month);
        //3.设置查询条件
        OrderSettingExample ose=new OrderSettingExample();
        OrderSettingExample.Criteria osec = ose.createCriteria();
        osec.andOrderdateBetween(firstDayOfMonth,lastDayOfMonth);
        List<OrderSetting> settings = orderSettingMapper.selectByExample(ose);
        //4.遍历结果集
        if (settings!=null && settings.size()>0){

            //将数据封装成下面的格式返回出去
        //{ date: 1, number: 120, reservations: 1 },{......},{......}
            List<Map> list=new ArrayList<Map>();
            for (OrderSetting os:settings) {
                Map map = new HashMap();
                map.put("date",os.getOrderdate().getDate());//获取到的年月日，只需要日子数
                map.put("number",os.getNumber()==null?0:os.getNumber());
                map.put("reservations",os.getReservations()==null?
                        0:os.getReservations());
                list.add(map);
            }
            //4.2 返回新的结果集
            return list;
        }
        return null;
    }

    @Override
    public void update(OrderSetting orderSetting) throws CustomException {
        long count = orderSettingMapper.findCountByOrderDates(orderSetting.getOrderdate());
        OrderSetting setting = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderdate());


        System.out.println(count+"left --- join"+orderSetting.getNumber()+"left --- join"+orderSetting.getReservations());
        if(count>0){
            if(setting.getReservations()>orderSetting.getNumber()){
                //当前日期已经进行了预约设置，需要进行修改操作
                throw new CustomException(MessageConstant.ORDERSETTING_FAILS);
            }else{
                //当前日期没有进行预约设置，进行添加操作
                orderSettingMapper.editNumberByOrderDate(orderSetting);
            }

        }else{
            orderSettingMapper.insertSelective(orderSetting);
        }

    }
}
